require(['common','contact','formedit','serviceSelect'], function () {
  // 底部详情
  var priceData = {
    list: [
      {con: '签证人数',price: 89999,num: 1},
    ],
    discounts: [
      {con: '弹出根据内容自适应高度300~550pt弹出根据内容自适应高度300~550pt',price: 2000},
    ]
  };
  var priceDetail = _.template($("#footbar-popup").html())(priceData);
  $(".gzl-footbar__bd").footPopup({
    title: '费用明细',
    titleAlign: 'center',
    content: priceDetail
  });
  // 成人、儿童、长者
  $(".booking-numlist .gzl-count span").on("click", function () {
    if (!$(this).hasClass("disabled")) {
      var numlist, total, max, min, num, val, type, selTpl, sellist;
      numlist = $(this).parents(".booking-numlist");
      total = numlist.data("total");
      max = numlist.data("max");
      min = numlist.data("min");
      num = $(this).parent().find(".gzl-count__num");
      val = parseInt(num.text());
      type = $(this).parent().data("type");
      selTpl = _.template($("#tourist-select").html())({ type: type });
      sellist = $(".tourist-sel-list");
      if ($(this).hasClass("gzl-count__plus")) {// 加
        if (total < max) {
          $(this).parent().find(".gzl-count__minus").removeClass("disabled");
          num.text(val + 1);
          numlist.data("total",total + 1);
          if (total + 1 === max) {
            numlist.find(".gzl-count__plus").addClass("disabled");
          }
          if (type === "adult") {
            if ($("[data-type='adult']", sellist).length < 1) {
              $(selTpl).prependTo(sellist);
            }
          } else if (type === "child") {
            if ($("[data-type='child']", sellist).length < 1) {
              if ($("[data-type='adult']", sellist).length < 1) {
                $(selTpl).prependTo(sellist);
              } else {
                $("[data-type='adult']", sellist).after(selTpl);
              }
            }
          } else {
            if ($("[data-type='elder']", sellist).length < 1) {
              sellist.append(selTpl);
            }
          }
        }
      } else if ($(this).hasClass("gzl-count__minus")) {// 减
        if (total > min) {
          numlist.find(".gzl-count__plus").removeClass("disabled");
          num.text(val - 1);
          numlist.data("total",total - 1);
          if (val - 1 === 0) {
            $(this).addClass("disabled");
            console.log(type)
            if (type === "adult") {
              console.log(1)
              $("[data-type='adult']", sellist).remove();
            } else if (type === "child") {
              console.log(2)
              $("[data-type='child']", sellist).remove();
            } else {
              console.log(3)
              $("[data-type='elder']", sellist).remove();
            }
          }
        }
      }
    }
  })
  // 联系人选择
  $(".tourist-sel-list").on("click", "li", function () {
    $.contact({
      type: $(this).data("type"),
      totality: 0,
      list: [{
        index: 0,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺·罗纳', mobile: '13800138000',cardTypeName: '身份证',cardNm: '441246195505058984'
      }, {
        index: 1,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺', mobile: '13800138001',cardTypeName: '身份证',cardNm: '440111198410201100'
      }, {
        index: 2,checkType: null, checkState: 'uncheck',name: '罗纳', mobile: '13800138002',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }, {
        index: 3,checkType: null, checkState: 'uncheck',name: '测试',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }],
      target: $(".booking-list-bd>li"),
      confirmFn: function (tourist,self) {
        // 确定按钮模板;
        var touristList = $(".tourist-select");
        var tpl = _.template($("#tourist-list").html())(tourist);
        // if($("li",touristList).length < 1){
        //   $(tpl).appendTo(touristList);
        // }else if(tourist.type === 'adult'){
          $(".tourist-info-list").remove();
          $(touristList).append($(tpl))
        // }
        // else if(tourist.type === 'child'){
        //   $(".child-info",touristList).remove();
        //   if($(".adult-info",touristList).length < 1){
        //     $(tpl).prependTo(touristList);
        //   } else {
        //     $(".adult-info",touristList).after(tpl);
        //   }
        // }else if(tourist.type === 'elder'){
        //   $(".elder-info",touristList).remove();
        //   touristList.append(tpl);
        // }
        $(".contact-select-page").hidePopup();
      },
      editsaveFn: function () {
        console.log(123123123123)
        $(".gzl-formedit").hidePopup();
      }
    });
  })
  // 使用日期
  $(".use-date").on("click", function () {
    var self = $(this);
    $.pushHistory();
    $.calendar({
      self: self,
      isFull: false,
      curDate: self.data("date"),
      date: {
        '2018-11-11': { holiday: '节日', tag: 'qiang', price: parseInt(Math.random() * 10000) },
        '2018-11-28': { holiday: '节日', tag: 'qiang', price: parseInt(Math.random() * 10000) },
        '2018-11-29': { holiday: '节日', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2018-12-01': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000) },
        '2018-12-02': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000), num: 0 },
        '2018-12-03': { holiday: '节日', tag: 'jian', price: parseInt(Math.random() * 10000) },
        '2018-12-30': { holiday: '休', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2018-12-31': { holiday: '休', tag: 'cu', price: parseInt(Math.random() * 10000) },
        '2019-01-01': { holiday: '元旦', tag: 'qiang', price: parseInt(Math.random() * 10000) }
      },
      gridClick: function (date, self) {
        console.log(date);
        self.removeClass("placeholder");
      }
    })
  })
  // 返回监听
  $.popstate(function () {
    var calendar = $(".gzl-calendar_popup").length < 1;
    var contactSel = $(".contact-select-page").length < 1;
    var contactEdit = $(".gzl-formedit").length < 1;
    var simulatePage = $(".simulatePage").length <1;
    var serviceSel = $(".service-select-page").length < 1;
    if (calendar && contactSel && contactEdit && simulatePage && serviceSel) {
      window.history.go(-1);
    } else {
      if (!contactEdit) {
        $(".gzl-formedit").hidePopup();
      } else {
        if(!calendar){
          $.calendarHide();
        }
        if(!contactSel){
          $(".contact-select-page").hidePopup();
        }
        if(!serviceSel) {
          $(".service-select-page").hidePopup();
        }
      }
      // 模拟页remove();
      if(!simulatePage){
        $(".simulatePage").remove();
      }
    }
  })

  // 积分
  $(".goIntegralUsePage").on("click",function(){
    // todoSomething
    integralUsePage();
  })
  // 优惠券
  $(".goCouponUsePage").on("click",function(){
    // todoSomething
    couponUsePage();
  })
  // 促销
  $(".openPromotionPopUp").on("click",function(){
    promotionPopUp();
  })

  // 可选服务弹出
  $(".open-chooseable-series").on("click",function() {
    $.serviceSelect({
      confirmFn: function() {
        $(".service-select-page").hidePopup();
        window.history.go(-1);
      }
    });
  })

  var serviceDetailTpl = _.template($("#service-selected").html())();
  $(".open-chooseable-series").after(serviceDetailTpl);

  $(".perRD-sel-item").find("em").on("click",function() {
    var tpl = _.template($("#perRD-sel-detail").html())();
      $.popup({
        title: '早餐',
        content: tpl,
        btnEvent: function() {

        }
      });
  })

  $(".perR-sel-item").find("em").on("click",function() {
    var tpl = _.template($("#perR-sel-detail").html())();
    $.popup({
      title: '迪士尼门票',
      content: tpl,
      btnEvent: function() {

      }
    });
  })

  $(".serviceGroup-sel-item").find("em").on("click",function() {
    var tpl = _.template($("#serviceGroup-sel-detail").html())();
    $.popup({
      title: '房间装饰主题',
      content: tpl,
      btnEvent: function() {

      }
    });
  })
});